﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SQLite;
namespace LuZhouLaoJiao
{
    public partial class CreateNormalUser : Form
    {
        public CreateNormalUser()
        {
            InitializeComponent();
        }

        private void btnCreateNormalUser_Click(object sender, EventArgs e)
        {
//             Global.dbUser = Global.getRegeditValue("localDBUser");
//             Global.dbPwd = Global.getRegeditValue("localDBPwd");
//             Global.database = Global.getRegeditValue("localDatabase");
//             Global.localDBIP = Global.getRegeditValue("localIP");

            string userName = tbUserName.Text.ToString().Trim();
            string userPwd = tbUserPwd.Text.ToString().Trim();
            string userPwd2 = tbUserPwd2.Text.ToString().Trim();
            if (tbUserName.Text.ToString().Trim() == "" || tbUserPwd.Text.ToString().Trim() == "" || tbUserPwd2.Text.ToString().Trim() == "")
            {
                MessageBox.Show("所有内容均为必填项！");
            }
            else if (!userPwd.Equals(userPwd2))
            {
                MessageBox.Show("输入的密码不一致！");
            }
            else if (Global.isUserExist(userName))
            {
                MessageBox.Show("该用户名已经存在，请使用另一个用户名.");
            }
            else if (userPwd.Length < 6)
            {
                MessageBox.Show("密码长度至少为六个字符");
            }
            else
            {
                //string strCon = "server = " + Global.localDBIP + "; user id = " + Global.dbUser + "; password = " + Global.dbPwd + "; database = " + Global.database + "; persist security info = false";
                //MySqlConnection conn = new MySqlConnection(strCon);
                //MySqlCommand comm = new MySqlCommand();
                //comm.Connection = conn;
                //comm.CommandText = "insert into user values(null,'" + userName + "','" + Global.MD5(userPwd.Trim()) + "','0');";
                //try
                //{
                //    conn.Open();
                //    comm.ExecuteNonQuery();
                //    Global.setRegeditValue("AlreadySetLocal", "YES");
                //    MessageBox.Show("已创建" + userName + "账户.");
                //}
                //catch (System.Exception ex)
                //{
                //    MessageBox.Show("数据库写入错误(CNU)：" + ex.Message);
                //}
                //finally
                //{
                //    conn.Close();
                //    this.Close();
                //}

                SQLiteConnection conn = new SQLiteConnection("Data Source=./data.db");
                SQLiteCommand comm = new SQLiteCommand();
                //MessageBox.Show("sqlite");
                conn.Open();
                comm = conn.CreateCommand();
                comm.CommandText = "insert into user values(null,'" + userName + "','" + Global.MD5(userPwd.Trim()) + "','0');";
                int i = comm.ExecuteNonQuery();
                if (i > 0)
                {
                    MessageBox.Show("用户" + userName + "创建成功.");
                    Global.setRegeditValue("AlreadySetAdmin", "YES");
                }
                else
                {
                    MessageBox.Show("用户" + userName + "创建失败.");
                }
                conn.Close();
                this.Close();
            }
        }
    }
}
